package com.stx.xhb.demo;

import static android.view.View.inflate;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.google.android.material.snackbar.Snackbar;
import com.hardbacknutter.sshd.SshdService;
import com.hardbacknutter.sshd.SshdSettings;
import com.hardbacknutter.sshd.StartMode;
import com.stx.xhb.demo.entity.LocalPicInfo;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.entity.LocalImageInfo;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * XBanner 使用在引导页控件
 */
public class GuideActivity extends AppCompatActivity {

    private final String LOG_TAG = "GuideActivity";

    private Context mContext;
    private static final int PERMISSION_REQUEST_CODE = 100;

    public static boolean isShow = false;

    public boolean canUserRoll =false;

    private View touch = null;

    private XBanner mXBanner;
    private FTPClient ftpClient = null;
    private FtpThread ftpThread = null;

    private String FTP_HOSTNAME = "192.178.5.251";
    private int FTP_PORT = 21;
    private String FTP_USERNAME ="adjr";
    //private String FTP_USERNAME = "adyy";
    private String FTP_PASSWORD = "Xy525300!";

    private int autoPlayTime = 30;

    FTPFile[] serverFileList = null;
    FTPFile[] serverConfList = null;

    List<FTPFile> downloadFileList = null;
    List<File> deleteFileList = null;
    File[] localFileList = null;

    FTPFile[] serverBranchFileList = null;
    List<FTPFile> downloadBranchFileList = null;
    List<File> deleteBranchFileList = null;
    File[] localBranchFileList = null;

    List<File> localShowFileList = null;

    private String downloadFilePath = "";
    private String downloadBranchFilePath = "";

    private int ftpStatus = -1;
    private final static int FTP_INIT_OK = 0;
    private final static int FTP_INIT_FAIL = 1;
    private final static int FTP_GET_LIST_OK = 2;
    private final static int FTP_GET_LIST_FAIL = 3;
    private final static int FTP_GET_FILE = 4;
    private final static int FTP_GET_FILE_OK = 5;
    private final static int START_FTP_SYNC = 6;

    private MainHandler mainHandler = new MainHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
        isShow = true;

        setContentView(R.layout.activity_splash);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        initView();

        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(mContext)) {
                //没有悬浮窗权限,去开启悬浮窗权限
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                    startActivityForResult(intent, 101);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        } else {
            mainHandler.sendEmptyMessageDelayed(START_FTP_SYNC, 5 * 1000);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        isShow = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isShow = false;
    }

    public static int getAutoPlayTime(Context mContext) {
        SharedPreferences mSharedPreferences = mContext.getSharedPreferences("Config", Context.MODE_PRIVATE);
        int time = mSharedPreferences.getInt("auto_play_time", -1);
        return time;
    }

    public static void setAutoPlayTime(Context mContext, int time) {
        SharedPreferences mSharedPreferences = mContext.getSharedPreferences("Config", Context.MODE_PRIVATE);
        mSharedPreferences.edit().putInt("auto_play_time", time).commit();
    }

    public static String getSystemProperty(String key, String defValue) {
        try {
            Class clz = Class.forName("android.os.SystemProperties");
            Method get = clz.getDeclaredMethod("get", String.class, String.class);
            return String.valueOf(get.invoke(clz, key, defValue));
        } catch (Exception e) {
            e.printStackTrace();
            return defValue;
        }
    }

    public String getCurrentBranch() {
        String branch = getSystemProperty("persist.sys.branch", "");
        return branch;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE: {
                // 检查授权结果
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    runFtpThread();
                } else {
                    Toast.makeText(mContext, "获取授权失败！请重新进入并且授权！", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            }
        }
    }

    private void initView() {
        touch = findViewById(R.id.touch);
        touch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (canUserRoll)
                    return false;
                else
                    return true;
            }
        });

        mXBanner = findViewById(R.id.xbanner);

        //Glide.get(mContext).clearMemory();
        //Glide.get(mContext).clearDiskCache();

        localShowFileList = new ArrayList<File>();

        initLocalFileList();
        initLocalBranchFileList();

        List<LocalPicInfo> localPicInfoList = new ArrayList<>();

        if (localFileList.length > 0 || localBranchFileList.length > 0) {
            for (File file : localFileList) {
                if (file.isFile()) {
                    localPicInfoList.add(new LocalPicInfo(file.getAbsolutePath()));
                    localShowFileList.add(new File(file.getAbsolutePath()));
                }
            }

            for (File file : localBranchFileList) {
                if (file.isFile()) {
                    localPicInfoList.add(new LocalPicInfo(file.getAbsolutePath()));
                    localShowFileList.add(new File(file.getAbsolutePath()));
                }
            }

            int time = getAutoPlayTime(mContext) * 1000;
            if (time > 0) {
                mXBanner.setAutoPlayTime(time);
            }

            mXBanner.setAutoPlayAble(true);
            mXBanner.setBannerData(localPicInfoList);
            mXBanner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {

                    Glide.with(mContext) // 使用Glide的上下文
                            .load(localShowFileList.get(position).getAbsolutePath()) // 加载文件
                            .into((ImageView) view);
                    canUserRoll = true;
                    Log.d(LOG_TAG, "display index " + position + " file =" + localShowFileList.get(position).getAbsolutePath());
                }
            });
        } else {
            List<LocalImageInfo> localImageInfoList = new ArrayList<>();
            localImageInfoList.add(new LocalImageInfo(R.mipmap.chinapost));
            mXBanner.setBannerData(localImageInfoList);
            mXBanner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {
                    ((ImageView) view).setImageResource(((LocalImageInfo) model).getXBannerUrl());
                }
            });
        }
    }

    public void initLocalFileList() {
        File externalFile = mContext.getExternalFilesDir(null);
        downloadFilePath = externalFile.getAbsolutePath();
        if (externalFile != null && externalFile.listFiles() != null)
            localFileList = externalFile.listFiles();
        else
            localFileList = null;
    }

    public void initLocalBranchFileList() {
        File externalFile = mContext.getExternalFilesDir(null);
        downloadBranchFilePath = externalFile.getAbsolutePath() + "/" + getCurrentBranch();

        File externalBranchFile = new File(mContext.getExternalFilesDir(null) + "/" + getCurrentBranch());

        if (externalBranchFile != null && externalBranchFile.listFiles() != null)
            localBranchFileList = externalBranchFile.listFiles();
        else {
            externalBranchFile.mkdir();
            localBranchFileList = new File[0];
        }
    }

    public void checkDownloadDelFile() {

        deleteFileList = new ArrayList<File>();
        downloadFileList = new ArrayList<FTPFile>();

        for (File lfile : localFileList) {
            // 处理每个文件
            //Log.d(LOG_TAG, "lfile=" + lfile.getName());
            FTPFile tmpfile = null;
            boolean foundFile = false;

            if (!lfile.isFile()) {
                continue;
            }

            for (FTPFile sfile : serverFileList) {
                tmpfile = sfile;
                //Log.d(LOG_TAG, "sfile=" + sfile.getName());

                if (!sfile.isFile())
                    continue;

                try {
                    if (compereByteArray(sfile.getName().getBytes("UTF-8"), lfile.getName().getBytes("UTF-8")) && lfile.length() == sfile.getSize()) {
                        foundFile = true;
                        break;
                    } else {
                        foundFile = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (!foundFile) {
                Log.d(LOG_TAG, "need to delete file =" + lfile.getName());
                deleteFileList.add(lfile);
            }
        }

        for (FTPFile sfile : serverFileList) {
            // 处理每个文件
            //Log.d(LOG_TAG, "lfile=" + sfile.getName());
            boolean foundFile = false;

            if (!sfile.isFile()) {
                continue;
            }

            for (File lfile : localFileList) {
                // 处理每个文件
                //Log.d(LOG_TAG, "sfile=" + sfile.getName());

                try {
                    if (compereByteArray(sfile.getName().getBytes("UTF-8"), lfile.getName().getBytes("UTF-8")) && lfile.length() == sfile.getSize()) {
                        foundFile = true;
                        break;
                    } else {
                        foundFile = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (!foundFile) {
                Log.d(LOG_TAG, "need to download file =" + sfile.getName());
                downloadFileList.add(sfile);
            }
        }
    }

    public void checkDownloadDelBranchFile() {

        deleteBranchFileList = new ArrayList<File>();
        downloadBranchFileList = new ArrayList<FTPFile>();

        for (File lfile : localBranchFileList) {
            // 处理每个文件
            //Log.d(LOG_TAG, "lfile=" + lfile.getName());
            FTPFile tmpfile = null;
            boolean foundFile = false;

            if (!lfile.isFile()) {
                continue;
            }

            for (FTPFile sfile : serverBranchFileList) {
                tmpfile = sfile;
                //Log.d(LOG_TAG, "sfile=" + sfile.getName());

                if (!sfile.isFile())
                    continue;

                try {
                    if (compereByteArray(sfile.getName().getBytes("UTF-8"), lfile.getName().getBytes("UTF-8")) && lfile.length() == sfile.getSize()) {
                        foundFile = true;
                        break;
                    } else {
                        foundFile = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (!foundFile) {
                Log.d(LOG_TAG, "need to delete branch file =" + lfile.getName());
                deleteBranchFileList.add(lfile);
            }
        }

        for (FTPFile sfile : serverBranchFileList) {
            // 处理每个文件
            //Log.d(LOG_TAG, "lfile=" + sfile.getName());
            boolean foundFile = false;

            if (!sfile.isFile()) {
                continue;
            }

            for (File lfile : localBranchFileList) {
                // 处理每个文件
                //Log.d(LOG_TAG, "sfile=" + sfile.getName());

                try {
                    if (compereByteArray(sfile.getName().getBytes("UTF-8"), lfile.getName().getBytes("UTF-8")) && lfile.length() == sfile.getSize()) {
                        foundFile = true;
                        break;
                    } else {
                        foundFile = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (!foundFile) {
                Log.d(LOG_TAG, "need to download branch file =" + sfile.getName());
                downloadBranchFileList.add(sfile);
            }
        }


    }

    public void downloadAndDelFile() {

        for (File lfile : deleteFileList) {
            Log.d(LOG_TAG, "del local file=" + lfile.getName());
            if (lfile.exists())
                lfile.delete();
        }

        try {
            ftpClient.changeToParentDirectory();
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (FTPFile sfile : downloadFileList) {

            try {
                String fileName = sfile.getName();
                String fileNameUTF8 = new String(fileName.getBytes("UTF-8"), "UTF-8");
                Log.d(LOG_TAG, "download sfile=" + fileName);

                File localFile = new File(downloadFilePath + "/" + fileNameUTF8);

                OutputStream outputStream = new FileOutputStream(localFile);

                //boolean success = ftpClient.retrieveFile(new String(sfile.getName().getBytes("GB2312"),"UTF-8"), outputStream);
                boolean success = ftpClient.retrieveFile(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"), outputStream);
                Log.d(LOG_TAG, "download ok? = " + success);

                if (!success)
                    Log.d(LOG_TAG, "download error code = " + ftpClient.getReplyCode() + " reply" + ftpClient.getReply());

                outputStream.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public void downloadAndDelBranchFile() {

        for (File lfile : deleteBranchFileList) {
            Log.d(LOG_TAG, "del local branch file=" + lfile.getName());
            if (lfile.exists())
                lfile.delete();
        }

        try {
            ftpClient.changeWorkingDirectory(new String((getCurrentBranch() + "/").getBytes("UTF-8"), "ISO-8859-1"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (FTPFile sfile : downloadBranchFileList) {

            try {
                String fileName = sfile.getName();
                String fileNameUTF8 = new String(fileName.getBytes("UTF-8"), "UTF-8");
                Log.d(LOG_TAG, "download branch sfile=" + fileName);

                File localFile = new File(downloadBranchFilePath + "/" + fileNameUTF8);

                OutputStream outputStream = new FileOutputStream(localFile);

                //boolean success = ftpClient.retrieveFile(new String(sfile.getName().getBytes("GB2312"),"UTF-8"), outputStream);
                boolean success = ftpClient.retrieveFile(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"), outputStream);
                Log.d(LOG_TAG, "download ok? = " + success);

                if (!success)
                    Log.d(LOG_TAG, "download error code = " + ftpClient.getReplyCode() + " reply" + ftpClient.getReply());

                outputStream.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public void onBackPressed() {
        Log.d(LOG_TAG, "onBackPressed!");
        showConfirmPsdDialog();
    }



    public void runFtpThread() {

        try {
            SshdSettings.writePasswordFile(this, "lwh", true, "525300");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Log.d(LOG_TAG, "start SshdService services!");

        if (SshdService.isRunning()!= true) {
            Intent intent = new Intent(mContext, SshdService.class);
            startService(intent);
        }

        if (ftpThread != null) {
            ftpThread.interrupt();
            ftpThread = null;
        }

        ftpThread = new FtpThread();
        ftpThread.start();
    }

    class MainHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case START_FTP_SYNC:
                    Log.d(LOG_TAG, "is time to start ftp check new version!");
                    ftpStatus = -1;
                    runFtpThread();
                    mainHandler.sendEmptyMessageDelayed(START_FTP_SYNC, 10 * 60 * 1000);
                    break;
                case FTP_INIT_OK:
                    Log.d(LOG_TAG, "init ftp ok!");
                    runFtpThread();
                    break;
                case FTP_INIT_FAIL:
                    Log.d(LOG_TAG, "ftp server connect fail!");
                    Toast.makeText(mContext, "服务器链接失败！", Toast.LENGTH_SHORT).show();
                    break;
                case FTP_GET_LIST_OK:
                    runFtpThread();
                    break;
                case FTP_GET_LIST_FAIL:
                    Log.d(LOG_TAG, "get ftp server file list fail!");
                    Toast.makeText(mContext, "获取文件列表失败！", Toast.LENGTH_SHORT).show();

                    int time = getAutoPlayTime(mContext) * 1000;
                    if (time > 0) {
                        mXBanner.setAutoPlayTime(time);
                    }

                    break;
                case FTP_GET_FILE:
                    runFtpThread();
                    break;
                case FTP_GET_FILE_OK:

                    Log.d(LOG_TAG, "download ok and start to auto play!");
                    initView();

                    break;

            }
        }
    }


    class FtpThread extends Thread {
        @Override
        public void run() {

            if (ftpStatus == -1) {

                Log.d(LOG_TAG, "start connect ftp server!");
                try {
                    ftpClient = new FTPClient();

                    ftpClient.connect(FTP_HOSTNAME, FTP_PORT);
                    ftpClient.setConnectTimeout(20 * 1000);
                    ftpClient.setDataTimeout(20 * 1000);
                    // 登录FTP服务器
                    boolean login = ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
                    if (login) {
                        // 登录成功
                        Log.d(LOG_TAG, "ftp server connect ok!");
                        ftpStatus = FTP_INIT_OK;
                        mainHandler.sendEmptyMessage(FTP_INIT_OK);
                    } else {
                        // 登录失败
                        Log.d(LOG_TAG, "ftp server connect fail!");
                        ftpStatus = FTP_INIT_FAIL;
                        mainHandler.sendEmptyMessage(FTP_INIT_FAIL);
                    }
                } catch (IOException e) {
                    ftpStatus = FTP_INIT_FAIL;
                    mainHandler.sendEmptyMessage(FTP_INIT_FAIL);
                    e.printStackTrace();

                }
            } else if (ftpStatus == FTP_INIT_OK) {
                try {
                    // 设置FTP文件传输模式为被动模式
                    ftpClient.enterLocalPassiveMode();
                    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                    ftpClient.setControlEncoding("UTF-8");

                    //获取文件列表
                    ftpClient.changeToParentDirectory();

                    // 获取FTP服务器上的广告切换时间配置
                    serverConfList = ftpClient.listFiles("conf/");

                    if (serverConfList != null && serverConfList.length > 0) {
                        for (FTPFile file : serverConfList) {
                            Log.d(LOG_TAG, "serverConfList=" + file.getName());
                            if (file.getName().startsWith("time_")) {
                                String timeStr = StringUtils.substringBetween(file.getName(), "time_", ".txt");
                                Log.d(LOG_TAG, "timeStr=" + timeStr);
                                int time = Integer.parseInt(timeStr);
                                Log.d(LOG_TAG, "get time=" + time);

                                if (time > 0)
                                    setAutoPlayTime(mContext, time);

                            }
                        }
                    }

                    serverFileList = ftpClient.listFiles();
                    Log.d(LOG_TAG, "sever get files size = " + serverFileList.length);

                    for (FTPFile file : serverFileList) {
                        Log.d(LOG_TAG, "fileName=" + file.getName() + " isFile=" + file.isFile() + " size=" + file.getSize());
                    }

                    serverBranchFileList = ftpClient.listFiles(new String(getCurrentBranch().getBytes("UTF-8"), "ISO-8859-1") + "/");
                    Log.d(LOG_TAG, "sever get branch files size = " + serverBranchFileList.length);

                    for (FTPFile file : serverBranchFileList) {
                        Log.d(LOG_TAG, "fileName=" + file.getName() + " isFile=" + file.isFile() + " size=" + file.getSize());
                    }

                    ftpStatus = FTP_GET_LIST_OK;
                    mainHandler.sendEmptyMessage(FTP_GET_LIST_OK);
                } catch (IOException e) {
                    mainHandler.sendEmptyMessage(FTP_GET_LIST_FAIL);
                    e.printStackTrace();

                    try {
                        ftpClient.disconnect();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            } else if (ftpStatus == FTP_GET_LIST_OK) {
                for (FTPFile file : serverFileList) {
                    // 处理每个文件
                    Log.d(LOG_TAG, "file=" + file.getName());
                }

                checkDownloadDelFile();
                checkDownloadDelBranchFile();

                if ((downloadFileList != null && deleteFileList != null) && downloadFileList.size() > 0 || deleteFileList.size() > 0 ||
                        (downloadBranchFileList != null && deleteBranchFileList != null) && downloadBranchFileList.size() > 0 || deleteBranchFileList.size() > 0) {
                    mXBanner.setAutoPlayAble(false);
                    canUserRoll = false;
                    ftpStatus = FTP_GET_FILE;
                    mainHandler.sendEmptyMessage(FTP_GET_FILE);
                    Log.d(LOG_TAG, "need to download or delete file!");
                } else {
                    Log.d(LOG_TAG, "no need to download or delete file!");

                    try {
                        ftpClient.disconnect();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            } else if (ftpStatus == FTP_GET_FILE) {
                Log.d(LOG_TAG, "download or delete file!");

                downloadAndDelFile();
                downloadAndDelBranchFile();

                Log.d(LOG_TAG, "download or delete end!");

                ftpStatus = FTP_GET_FILE_OK;

                Glide.get(mContext).clearDiskCache();

                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mainHandler.sendEmptyMessage(FTP_GET_FILE_OK);
            } else {

            }
        }
    }

    public static boolean compereByteArray(byte[] b1, byte[] b2) {

        if (b1.length == 0 || b2.length == 0) {
            return false;
        }

        if (b1.length != b2.length) {
            return false;
        }

        boolean isEqual = true;

        for (int i = 0; i < b1.length && i < b2.length; i++) {
            if (b1[i] != b2[i]) {
                isEqual = false;
                break;
            }
        }
        return isEqual;
    }


    private void showConfirmPsdDialog() {
        Log.d(LOG_TAG, "showConfirmPsdDialog!");
        //需要自己去定义对话框的显示样式，所以要调用dialog.setView(view);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final AlertDialog dialog = builder.create();
        final View view = inflate(mContext, R.layout.dialog_confirm_pwd, null);
        //让对话框显示一个自己定义的对话框界面效果
        dialog.setView(view);
        dialog.show();

        Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
        Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
        EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
        et_confirm_psd.requestFocus();
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
                String confirmPsd = et_confirm_psd.getText().toString();
                String psd = String.valueOf("525300");
                Log.v("PSD", confirmPsd);
                if (!confirmPsd.isEmpty()) {
                    if (psd.equals(confirmPsd)) {
                        finish();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(mContext, "密码错误，请联系管理员！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    //提示用户密码输入为空的情况
                    Toast.makeText(mContext, "密码输入为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

}
